package com.example.employeemanagement.Controller;

import com.example.employeemanagement.dto.LoginResponseDto;
import com.example.employeemanagement.entry.Employee;
import com.example.employeemanagement.service.AuthService;
import com.example.employeemanagement.service.EmployeeService;
import com.example.employeemanagement.util.JwtUtil;
import com.example.employeemanagement.vo.ApiResponse;
import com.example.employeemanagement.vo.LoginVo;
import com.example.employeemanagement.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private AuthService authService;
    
    @Autowired
    private JwtUtil jwtUtil;

    // http://localhost:8080/api/auth/login
    @PostMapping("/login")
    public ResponseEntity<ApiResponse<LoginResponseDto>> login(@RequestBody LoginVo user) {
        try {
            // 调用服务层方法进行登录验证并获取token
            LoginResponseDto loginResponse = authService.login(user);
            
            // 构建成功响应
            ApiResponse<LoginResponseDto> response = new ApiResponse<>("SUCCESS", "登录成功", loginResponse);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            // 构建错误响应
            ApiResponse<LoginResponseDto> response = new ApiResponse<>("ERROR", e.getMessage(), null);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        } catch (Exception e) {
            // 构建系统错误响应
            ApiResponse<LoginResponseDto> response = new ApiResponse<>("ERROR", "系统错误，请稍后重试", null);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
    
    // http://localhost:8080/auth/info


    // http://localhost:8080/auth/register
   @PostMapping("/register")
   public ResponseEntity<ApiResponse<String>> register(@RequestBody Employee user) {
       try {
           String result = authService.register(user);
           if (result.equals("注册成功")) {
               ApiResponse<String> response = new ApiResponse<>("SUCCESS", result, null);
               return ResponseEntity.ok(response);
           } else {
               ApiResponse<String> response = new ApiResponse<>("ERROR", result, null);
               return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
           }
       } catch (Exception e) {
           ApiResponse<String> response = new ApiResponse<>("ERROR", "系统错误，请稍后重试", null);
           return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
       }
   }
   
   // http://localhost:8080/api/auth/logout
   @PostMapping("/logout")
   public ResponseEntity<ApiResponse<Boolean>> logout(@RequestHeader("Authorization") String authHeader) {
       try {
           // 从Authorization头中提取token
           String token = null;
           if (authHeader != null && authHeader.startsWith("Bearer ")) {
               token = authHeader.substring(7);
           }
           
           // 调用服务层方法进行登出
           boolean logoutSuccess = authService.logout(token);
           
           // 构建成功响应
           ApiResponse<Boolean> response = new ApiResponse<>("SUCCESS", "退出登录成功", logoutSuccess);
           return ResponseEntity.ok(response);
       } catch (IllegalArgumentException e) {
           // 构建错误响应
           ApiResponse<Boolean> response = new ApiResponse<>("ERROR", e.getMessage(), false);
           return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
       } catch (Exception e) {
           // 构建系统错误响应
           ApiResponse<Boolean> response = new ApiResponse<>("ERROR", "系统错误，请稍后重试", false);
           return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
       }
   }
}